/*
 * Copyright(C) 2012 Luvina Software Company
 *
 * TelValidator.java, Jun 7, 2012 HaiPD
 */
package net.luvina.manageuser.validator;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;

import net.luvina.manageuser.util.Message;

/**
 * TelValidator
 *
 * @author HaiPD
 *
 */
public class TelValidator implements Validator {

	private static final String TEL_PATTERN = "\\d{1,}[-]{1}\\d{1,}[-]{1}\\d{1,}";

	private Pattern pattern;
	private Matcher matcher;

	/**
	 * Contructor Default
	 */
	public TelValidator() {
		pattern = Pattern.compile(TEL_PATTERN);
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see
	 * javax.faces.validator.Validator#validate(javax.faces.context.FacesContext
	 * , javax.faces.component.UIComponent, java.lang.Object)
	 */
	@Override
	public void validate(FacesContext context, UIComponent component,
			Object object) throws ValidatorException {
		String labelField = (String) component.getAttributes().get("label");
		matcher = pattern.matcher(object.toString());
		if (!matcher.matches()) {

			FacesMessage msg = new FacesMessage("「" + labelField
					+ "」 xxxx-xxxx-xxxx" + Message.getString("ER005"));
			msg.setSeverity(FacesMessage.SEVERITY_ERROR);
			throw new ValidatorException(msg);
		}
	}

}
